package com.xuedongzhilian.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
    private Boolean success;
    private String errorMsg;
    private Object data;
    private Long total;
    // 添加code字段以兼容前端期望的格式
    private Integer code;

    public static Result ok(){
        Result result = new Result(true, null, null, null);
        result.setCode(1); // 成功时code为1
        return result;
    }
    public static Result ok(Object data){
        Result result = new Result(true, null, data, null);
        result.setCode(1); // 成功时code为1
        return result;
    }
    public static Result ok(List<?> data, Long total){
        Result result = new Result(true, null, data, total);
        result.setCode(1); // 成功时code为1
        return result;
    }
    public static Result fail(String errorMsg){
        Result result = new Result(false, errorMsg, null, null);
        result.setCode(0); // 失败时code为0
        return result;
    }
    
    // 构造函数
    public Result(Boolean success, String errorMsg, Object data, Long total) {
        this.success = success;
        this.errorMsg = errorMsg;
        this.data = data;
        this.total = total;
        // 根据success设置code
        this.code = success ? 1 : 0;
    }
}